﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using com.fukefu.sql;

namespace com.fukefu.tools
{
    public class ExpandKeyword
    {
        Hashtable ht = new Hashtable();
        string[] separator = new string[] { "|", "," };
        Random random = new Random();

        public ExpandKeyword()
        {
            SqlServerOperate sql = new SqlServerOperate();
            //加载扩展词分类
            SqlDataReader re = sql.Query("select * from 全局设置.dbo.[_bt_扩展词分类]");
            while (re.Read())
            {
                ht.Add(re["expandid"].ToString(), new List<string>());
            }
            re.Close();
            //加载所有扩展词
            re = sql.Query("select a.word,a.expandid,b.word as type from [_bt_扩展词] as a,全局设置.dbo.[_bt_扩展词分类] as b where a.expandid=b.expandid");
            while (re.Read())
            {
                List<string> list = ht[re["expandid"].ToString()] as List<string>;
                if (re["type"].ToString().Equals("疑问词"))
                {
                    list.Add(re["word"].ToString());
                }
                else
                {
                    list.Add(separator[random.Next(separator.Length)] + re["word"].ToString());
                }
            }
            re.Close();
            sql.Close();
        }
        public string Get(string expandid)
        {
            if (ht.Contains(expandid))
            {
                List<string> list = ht[expandid] as List<string>;
                if (list.Count > 0)
                {
                    return list[random.Next(list.Count)];
                }
            }
            return "";
        }
    }
}
